<template>
  <div class="document-select">
    <el-form>
      <el-form-item label="单据分类">
        <el-select v-model="selectedCategory" placeholder="请选择单据分类">
          <el-option v-for="category in categories" :key="category.id" :label="category.name" :value="category.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-table :data="filteredDocuments" style="width: 100%">
      <el-table-column prop="name" label="单据名称" width="180"></el-table-column>
      <el-table-column prop="category" label="分类" width="180"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button type="text" @click="selectDocument(row)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button type="primary" @click="saveDocuments">保存</el-button>
  </div>
</template>
<script setup>
import { ref, computed } from 'vue';
import { ElSelect, ElOption, ElButton, ElTable, ElTableColumn, ElForm, ElFormItem } from 'element-plus';
import router from "@/router";
const categories = ref([
  { id: '1', name: '分类1' },
  { id: '2', name: '分类2' },
  // 更多分类...
]);
const documents = ref([
  { id: '1', name: '单据1', category: '分类1' },
  { id: '2', name: '单据2', category: '分类1' },
  // 更多单据...
]);
const selectedCategory = ref('');
const selectedDocuments = ref([]);

const filteredDocuments = computed(() => {
  return selectedCategory.value ? documents.value.filter(doc => doc.category === selectedCategory.value) : documents.value;
});

const selectDocument = (document) => {
  if (selectedDocuments.value.length < 10) {
    const index = selectedDocuments.value.findIndex(d => d.id === document.id);
    if (index === -1) {
      selectedDocuments.value.push(document);
    } else {
      selectedDocuments.value.splice(index, 1);
    }
  } else {
    alert('最多只能选择10个单据');
  }
};

const saveDocuments = () => {
  console.log('选中的单据:', selectedDocuments.value);
  router.push( '/main/home');
  alert('保存成功');
};

</script>
<style scoped>
.document-select {
  padding: 20px;
}

.el-form-item {
  margin-bottom: 10px;
}

.el-button {
  margin-top: 20px;
}
</style>